// pages/classify/classify.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 0,
    tabList: [],
    tabCartTotal: '',
    itemId: '',
    categoryId: '',
    isBaokuan: '',
    isPintuan: '',
    isMiaosha: '',
    isYushou: '',
    isYuyue: '',
    // 条件筛选
    isall: true,//综合
    issalesreal: '',//销量
    isNew: '',//新品
    ismarketprice: '',//价格
    isWritePriceShow: false, //控制背景显示隐藏
    clickShaixuan: false,//控制是否点击筛选文字，然后设置选中color
    minPrice: '',//最小价格
    maxPrice: '',//最大价格
    sidx: '',//排序字段，存储选择的是价格还是销量还是最新
    order: '',//升降序
  },
  onReady: function () {
    this.animation = wx.createAnimation()
  },
  /**
   * 页面滚动
   */
  onPageScroll(e) {
    this.selectComponent('#goodlist').cOnPageScroll(e);
  },

  //监听app全局变量变化回调
  watchBack: function (value) {
    this.setData({
      tabCartTotal: value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (event) {
    let ii = "#goodlist0";
    let _this = this;
    if (event.status == 'isBaokuan') {
      _this.setData({
        isBaokuan: 1,
      })
    } else if (event.status == 'isPintuan') {
      _this.setData({
        isPintuan: 1,
      })
    } else if (event.status == 'isMiaosha') {
      _this.setData({
        isMiaosha: 1,
      })
    } else if (event.status == 'isYushou') {
      _this.setData({
        isYushou: 1,
      })
    } else if (event.status == 'isYuyue') {
      _this.setData({
        isYuyue: 1,
      })
    }
    if (_this.selectComponent(ii))
      _this.selectComponent(ii).getGoodList(false, event.categoryId);
    this.setData({
      categoryId: event.categoryId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    app.watch(this.watchBack);//监听app全局变量
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.selectComponent("#goodlist").bottomList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 跳转搜索列表
  toSearch: function () {
    wx.navigateTo({
      url: '/pages/index/search/search?type=0'
    })
  },
  // 跳转所有分类
  jumpClassifyAll: function () {
    wx.navigateTo({
      url: '/pages/classify/classify-all/classify-all'
    })
  },
  // 选择商品状态
  selectStatus: function (e) {
    let status = e.currentTarget.dataset.status;
    let ii = "#goodlist0";
    let _this = this;
    if (status == 'isBaokuan') {
      _this.setData({
        isBaokuan: 1,
        isPintuan: '',
        isMiaosha: '',
        isYushou: '',
      })
    } else if (status == 'isPintuan') {
      _this.setData({
        isBaokuan: '',
        isPintuan: 1,
        isMiaosha: '',
        isYushou: '',
      })
    } else if (status == 'isMiaosha') {
      _this.setData({
        isBaokuan: '',
        isPintuan: '',
        isMiaosha: 1,
        isYushou: '',
      })
    } else if (status == 'isYushou') {
      _this.setData({
        isBaokuan: '',
        isPintuan: '',
        isMiaosha: '',
        isYushou: 1,
      });
    };
    if (_this.selectComponent(ii))
      _this.selectComponent(ii).getGoodList(false, _this.data.categoryId);
  },
  // 判断选择综合，销量，新品，价格
  selectFunction: function (e) {
    let ii = "#goodlist0";
    let status = e.currentTarget.dataset.status;
    let that = this;
    if (status == 'isall') {
      that.setData({
        issalesreal: '',
        isNew: '',
        ismarketprice: '',
        isall: true,
        clickShaixuan: false,
        minPrice: '',
        maxPrice: '',
        sidx:'',
        order:'',
      })
    } else if (status == 'issalesreal') {
      // 校验默认值是否为空，如果为空则将该值设置为true，然后开始切换true,false
      if (that.data.issalesreal == '') {
        that.setData({
          issalesreal: true,
          order: true,
        })
      } else {
        that.setData({
          issalesreal: !that.data.issalesreal,
          order: !that.data.order,
        })
      };
      that.setData({
        isNew: '',
        ismarketprice: '',
        isall: false,
        sidx:'salesreal',
      })
    } else if (status == 'isNew') {
      if (that.data.isNew == '') {
        that.setData({
          isNew: true,
          order: true,
        })
      } else {
        that.setData({
          isNew: !that.data.isNew,
          order: !that.data.order,
        })
      };
      that.setData({
        isall: false,
        issalesreal: '',
        ismarketprice: '',
        sidx:'isnew',
      })
    } else {
      if (that.data.ismarketprice == '') {
        that.setData({
          ismarketprice: true,
          order: true,
        })
      } else {
        that.setData({
          ismarketprice: !that.data.ismarketprice,
          order: !that.data.order,
        })
      };
      that.setData({
        issalesreal: '',
        isNew: '',
        isall: false,
        sidx:'marketprice',
      })
    };
    console.log(that.data.sidx+that.data.order);
    if (that.selectComponent(ii))
    that.selectComponent(ii).getGoodList(false, that.data.categoryId);
  },
  // 显示价格弹框
  selectPrice: function () {
    let that = this;
    that.setData({
      isWritePriceShow: true,
    })
  },
  //关闭价格弹框
  closePrice: function () {
    let that = this;
    that.setData({
      isWritePriceShow: false,
    })
  },
  // 获取最低价格
  selectMinPrice: function (e) {
    this.setData({
      minPrice: e.detail.value
    })
  },
  // 获取最高价格
  selectMaxPrice: function (e) {
    this.setData({
      maxPrice: e.detail.value
    })
  },
  //点击重置价格调用接口
  setInputValue: function () {
    this.setData({
      maxPrice: '',
      minPrice: '',
    })
  },
  //点击确定调用接口
  chooseInputValue: function () {
    let ii = "#goodlist0";
    let that = this;
    if (that.data.minPrice || that.data.maxPrice) {
      that.setData({
        clickShaixuan: true
      })
    }
    that.setData({
      isWritePriceShow: false
    });
    if (that.selectComponent(ii))
    that.selectComponent(ii).getGoodList(false, that.data.categoryId);
  },
})
